package com.ray.server;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Queue;
import com.ray.basic.BASIC;
import com.ray.server.RW_myQ;

public class Server_out  implements Runnable{
	private static Queue<String[]> myQ=new LinkedList<String[]>();
	public void run(){
		while(true){
			try {
				Thread.sleep(10);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			String[] info_array= new RW_myQ().get();
			if (info_array.length>0){
				String clientid= info_array[0];
				String information=info_array[1];
			    byte[] sendBuf= BASIC.pack(information);
				OutputStream out= new TCPEchoServer().get_map_out().get(clientid);
				Socket socket= new TCPEchoServer().get_map_socket().get(clientid);
				if (information.equals("$$END_FROM_CLIENT")) {
					try {
						socket.close();
						System.out.println("Server_out::"+clientid+":socket closed...");
						new TCPEchoServer().get_active_client().set(Integer.parseInt(clientid), "&&offline");
						new TCPEchoServer().get_map_out().remove(clientid);
						new TCPEchoServer().get_map_socket().remove(clientid);
					} catch (IOException e) {
						e.printStackTrace();
						break;
					}
				}
				try {
					out.write(sendBuf);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static Queue<String[]> get_myQ(){
		return myQ;
	}
}
